//前提
var mongoose = require("mongoose")

//引用插件
var mongoosePaginate = require('mongoose-paginate');

//约束
var schema = new mongoose.Schema({
  title: {
    type: String,
    required: [true, "标题不能为空"]
  },
  author: {
    type: mongoose.Schema.Types.ObjectId,
  },
  news_type: {
    type: mongoose.Schema.Types.ObjectId,
  },
  describe: {
    type: String,
    required: [true, "新闻描述不能为空"],
    maxlength: 200,
    minlength: [20, '新闻描述不得少于20字']
  },
  comment_num: { //评论数量
    type: Number
  },
  content: { //内容
    type: String,
    required: [true, "新闻内容不能为空"],
  },
  hot: Boolean, //火热程度
  pro_data: {  //发表时间
    type: Date,
    default: Date.now
  },
  tips_source: {  //源头地址
    type: String,
    required: [true, "转载地址不能为空"],
       validate:{
        validator: function(data) {
          return /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:\/~+#]*[\w\-@?^=%&\/~+#])?$/.test(data);
          },
        message: 'Url格式不正确' //VALUE代表phone存放的值
        },
      },
  keywords: {
    type: Array //新闻关键字
  }
});



//引入插件
schema.plugin(mongoosePaginate);

//实例化
module.exports = mongoose.model('News', schema, 'news'); // Model.paginate()